/*
 *  main.cpp
 *  joosVM
 *
 *  Created by Smári Waage on 11.6.2009.
 *  Copyright 2009 smari.org. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. *
 *
 */

#include <iostream>
#include <stdio.h>
#include "interpreter.h"

static double diffclock(clock_t clock1,clock_t clock2)
{
	double diffticks=clock1-clock2;
	double diffms=(diffticks)/(CLOCKS_PER_SEC/1000);
	return diffms;
}

void usage(){
	fprintf(stderr, "Usage: joosVM [-vm_options] classfile [args...]\n");
}

int main (int argc, char *  argv[]) {
	
	printf("JoosVM \n");
	
	int result = 1;
	clock_t	start = clock();
			
	//TODO: remove at some point,
	//when running directly from within xcode..
#if 0
		//Shortcut...
		ClassLoader::classpath = "/Users/smari/Documents/Workspaces/Xcode/vm/joosVM/testPrograms/";
		Flags::entryClass = "Test";
#else
		
	// Check arguments.
	if (argc < 2) {
		usage();
		exit(1);
	}
	
	Flags::SetFlagsFromCommandLine(argc,argv);
	
	if(Flags::classpath != NULL)
		ClassLoader::classpath = Flags::classpath;
	else{
		char directory[200];
		getcwd(directory, sizeof(directory));
		char* dir = (char*)calloc(strlen(directory) + 1, sizeof(char));
		strcat(dir,directory);
		strcat(dir,"/");
		ClassLoader::classpath = dir;
	}
		
#endif
	
	//Interpret
	if(Flags::entryClass != NULL)	
		result = Interpreter::interpret();
	else
		usage();
	
		
	clock_t end = clock();	
	printf("\nInterpretation took %g milliseconds.\n", diffclock(end, start));
	
	printf("\nexit code: %d\n", result);
	if (Flags::keepalive) {
		getchar();
	}
	//getchar();
	return result;
}



